# coding: utf-8

from io import StringIO
from typing import Sequence, Union
from graph import Graph, Subgraph, edge_set, vertex_t


"""
* Holds the edges that were reversed to remove cycles.
* This struct contains the edges in their reversed form
* i.e. if edge (u, v) cuased a cycle, it is saved as (v, u)
"""


class RevEdges:
    reversed: edge_set
    collapsed: edge_set
    loops: Sequence[vertex_t]

    def __init__(self, g: Union[Graph, Subgraph]):

        self.reversed = edge_set(g, "reversed", lambda: [])
        self.collapsed = edge_set(g, "collapsed", lambda: [])
        self.loops = []

    def __str__(self) -> str:
        out = StringIO()
        out.write(f"reversed: {self.reversed}\n")
        out.write(f"collapsed: {self.collapsed}\n")
        out.write(f'loops:[{",".join(map(str,self.loops))}]\n')
        return out.getvalue()
